$.extend(planner.roster,
{
    selectedMechs: [],
    lastHtml: "",
    show: function()
    {
        $("#roster").show(); $("#rostercontent").show(duration=0);
    },
    selected_players_names: function()
    {
        players = [];
        
         $('.cell-roster-grid').each(function( index, element ) {
            e = $(this);
            if ( e.hasClass("gradient") )
            {
                players.push(e.attr("name"));
            }
        });
         
        $('.row-roster-table').each(function( index, element ) {
            e = $(this);
            if ( e.hasClass("gradient") )
            {
                players.push(e.attr("name"));
            }
        });
        
        
        alert(players);
        
        return players;
    },
    store_selected_mechs: function()
    {
        console.log("planner.roster.store_selected_mechs()");
        planner.roster.selectedMechs = [];
        
        $('.cell-roster-grid, .row-roster-table').each(function( index, element ) {
            e = $(this);
            if ( e.hasClass("gradient") )
            {
                console.log("planner.roster.store_selected_mechs: selected: " + e.attr("id"));
                planner.roster.selectedMechs.push(e.attr("id") );
            }
        });
        
        console.log("planner.roster.store_selected_mechs: stored: " + planner.roster.selectedMechs.length);
    },
    restore_selected_mechs: function()
    {
        console.log("planner.roster.restore_selected_mechs()");
       // if ( planner.roster.selectedMechs.length )
        //    setTimeout(function() { $("#btnAssignLance").button("enable"); }, 10);
            //Q
            
        $.each(planner.roster.selectedMechs, function(index, id) {
            $("#"+id).addClass("gradient");
            console.log("planner.roster.restore_selected_mechs: id: " + id);
        });
        console.log("planner.roster.restore_selected_mechs: restored: " + planner.roster.selectedMechs.length);

    },
    ui: {
        init: function(){
            console.log("planner.roster.ui.init()");
        },
        setup: function(){
            console.log("planner.roster.ui.setup()");
            planner.roster.ui.setup_buttons();
            planner.roster.ui.setup_views();
        },
        is_visible: function()
        {
            return $("#roster").css("display") != "none";
        },
        
        auto_update: function()
        {
            console.log("planner.roster.ui.auto_update()");
            if ( planner.roster.ui.is_visible() )
            {
                $.ajax({ 
                    url: "/d",
                    type: "GET",
                    data: { d: planner.mech.dropid }
                })
                .done( function(data) {
                    if (data != planner.roster.lastUpdate || planner.roster.lastUpdate == "") {
                        console.log("planner.roster.ui.auto_update: changes data " + data + " != " + planner.roster.lastUpdate);
                        planner.roster.lastUpdate = data
                        planner.roster.ui.update(function(){ $("#roster").show(); $("#rostercontent").show(duration=0) });
                    }
                    else
                    {
                        console.log("planner.roster.ui.auto_update: no changes " + data + " == " + planner.roster.lastUpdate);
                    }
                }).fail( function() {
                    
                });
            
            }
        
            setTimeout(planner.roster.ui.auto_update, 5000);
        },
        
        update_html: function(html)
        {
                console.log("planner.roster.ui.update_html()");
                planner.roster.store_selected_mechs();
                $("#rosterdiv").html(html);
                console.log("planner.roster.ui.update: div updated");
                planner.roster.lastSortInverse = !planner.roster.lastSortInverse;
                planner.roster.ui.sort_table($("#" + planner.roster.lastSortId));
                if ( planner.roster.lastView == "table" ) {
                    $("#tblRoster").css("display", "block");    
                }
                else
                {
                    $("#gridRoster").css("display", "block");    
                }
                planner.roster.ui.setup();

                planner.roster.restore_selected_mechs();
                planner.roster.ui.set_button_state();
        },
        
        update: function(callback)
        {
            console.log("planner.roster.ui.update()");
            var v = 
                (
                    $( "#tutorials" ).css( "display" ) == "block" ||
                    $( "#overview" ).css( "display" ) == "block"  ||
                    $( "#download" ).css( "display" ) == "block"  ||
                    $( "#faq" ).css( "display" ) == "block"                    
                )
                ? true : false;
            
            if (v)
                return;
                
            console.log("planner.roster.ui.update: posting mech");
            planner.server.post_mech({ i:"1" }, function (data) {
                console.log("planner.roster.ui.update: posted");
                planner.roster.ui.update_html(data);

                if (callback) 
                       callback();
            });
        },
        sort_table: function(element)
        {
            console.log("planner.roster.ui.sort_table(" + element + ")");
            planner.roster.lastSortId = element.attr("id");
            
            $('#tblRoster').find('td').filter(function(){
                
                return $(this).index() === element.index();
                
            }).sortElements(function(a, b){
                
                return $.text([a]) > $.text([b])
                    ? planner.roster.lastSortInverse ? -1 : 1
                    : planner.roster.lastSortInverse ? 1 : -1;
                
            }, function(){
                
                // parentNode is the element we want to move
                return this.parentNode; 
                
            });
            
            planner.roster.lastSortInverse = !planner.roster.lastSortInverse;
                
        },
        post_action: function  (action, callback)
        {
            console.log("planner.roster.ui.post_action(" + action + ")");
            var payload = { 
                d:          planner.mech.dropid, 
                p:          planner.mech.player, 
                a:          action,
                i:          1,
                players:     planner.roster.selected_players_names().join(",")
            }
            
            $.ajax({ 
                url: "/",
                type: "POST",
                data: payload
            })
            .done( function(data) {
               
                planner.roster.ui.update_html(data);
                planner.roster.show();
                planner.drop.ui.remove_selections();
                planner.roster.ui.disable_buttons_that_need_selection();
                
                if (callback)
                    callback();
            });
        },
        disable_buttons_that_need_selection: function() {
            //$("#btnGarageOf").button("disable");
            $("#btnAssignLance").hide();
//            $("#btnRemovePlayer").button("disable");
        },
        enable_buttons_that_need_selection: function() {
           // $("#btnGarageOf").button("enable");
            $("#btnAssignLance").show();
            //$("#btnRemovePlayer").button("enable");
        }
    }
});


/* 

//setTimeout( function() { $('#mnuAssignLance').menu({ position: { my: "center top", at: "top+25" }}).hide(); }, 10 );












        
        

    
//$(".cell-roster-table-tooltip").hover( 
//    function() {
//        planner.drop.ui.select_div( $(this).parent(),  false, false );
//    },
//    function() {
//        planner.drop.ui.deselect_div( $(this).parent(), false, false );
//    }
//);
//
//
//$(".row-roster-table").hover( 
//    function() {
//        planner.drop.ui.select_div( $(this), false, false );
//    },
//    function() {
//        planner.drop.ui.deselect_div( $(this), false, false );
//    }
//);



$( ".row-roster-table" ).tooltip({ content: "Awesome title!" });
$( ".cell-roster-table-tooltip" ).tooltip({ content: "Awesome title!" });





function scrollPlayername (id)
{
    span = $("#mrpt" + id);
    div  = $("#mlyr" + id);
    l = parseInt(span.css("left"));
    w = parseInt(span.width());
    m = parseInt(div.width());
    
    if (w > m)
    {
        if (pScroll)
        {
            l = l - 2
            
            if ( l + w - parseInt(m/2) < 0 )
                l = 0
        
            setTimeout(function() { scrollPlayername(id) }, 100);
        }
        else
        {
            l = 0;
        }
    
        span.css("left", l+"px")
    }
    
    //alert(gScroll + ":" + l + "/" + w + "/" + m);
}


// {% for mech in mechs %}

pScroll = false;
//alert("#mscrl{{ mech.player|escapejs }}");

$("#mscrl{{ mech.player|toID }}").hover(
    function(){
        pScroll = true;
        scrollPlayername("{{ mech.player|toID}}");
    },function(){
        pScroll = false;
        scrollPlayername("{{ mech.player|toID }}");
    }
);

// {% endfor %}








{% endif %}





</script>

*/
